package nl.scalasim

import scala.actors.Actor

class Citizen(name: String) extends ScalasimActor(name) {

  var workplace: Actor = _

  def workAt(w: Actor) {
    workplace = w
  }

  def act() {
    loop {
      react (handleAct orElse handleDefault)
    }
  }

  /** Handler for Act messages. */
  def handleAct(): PartialFunction[Any, Unit] = {
    case Act if workplace == null => {
      warn("Cannot act, no workplace")
      reply(Idle)
    }

    case Act => {
      val caller = sender
      info("Harvesting from %s", workplace)
      workplace ! Harvest
      react {
        case Harvested(_, _) => {
          caller ! Working
        }
      }
    }

    case Harvested(_, _) => { /* ignore */ }
  }

}